package com.esms.controller;

import com.esms.po.Department;
import com.esms.po.DepartmentWithResp;
import com.esms.service.DepartmentService;
import com.esms.service.IResponsibilityService;
import com.esms.utils.Message;
import com.esms.vo.DepartmentPages;
import com.esms.vo.ResponsibilityChangeVo;
import com.esms.vo.ResponsibilityDepVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 部门Controller
 *
 * @Author: ko
 * @DATE: 2022/1/24 15:23
 */

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    public DepartmentService departmentService;

    @Autowired
    IResponsibilityService responsibilityService;


    @RequestMapping("/superiorDepartments")
    @ResponseBody
    public List<Department> superiorDepartments(){
        return  departmentService.ListSuperiorDepartments();
    }


    /**
     * 查找部门
     *
     * @param pageNum
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/findSelective.do")
    @ResponseBody
    public DepartmentPages findSelective(
            @RequestParam(value = "page", defaultValue = "1") int pageNum,
            @RequestParam(value = "limit", defaultValue = "5") int limit,
            @RequestParam(value = "name", defaultValue = "") String name) throws Exception {
        List<DepartmentWithResp> list;
        //模糊查询，有多少个条件就接收多少个字段
        Department department = new Department();
        department.setdName(name);
        //pageNum:起始页面  pageSize:每页的大小
        PageHelper.startPage(pageNum, limit);
        //查找条件，一定要紧跟在startPage后
        list = departmentService.findSelective(department);
        PageInfo pageResult = new PageInfo(list);
        //设置前台需要的数据
        DepartmentPages departmentPages = new DepartmentPages();
        departmentPages.setCode(0);
        departmentPages.setMsg("");
        departmentPages.setCount((int) pageResult.getTotal());
        departmentPages.setData(pageResult.getList());
        return departmentPages;
    }

    /**
     * 添加部门
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Message add(String name, String remark, String level, String gsid, String resp) throws Exception {
        Department department = departmentService.findByDname(name);
        //查找是否同名
        if (department != null) {
            return Message.error("部门名称已存在");
        }
        if (StringUtils.isEmpty(name)) {
            return Message.error("请填写部门名称");
        }

        Department department2 = new Department();
        department2.setdId(null);
        department2.setdName(name);
        department2.setdRemark(remark);
        department2.setdLevel(Integer.valueOf(level));
        department2.setdGsid(Integer.valueOf(gsid));
        department2.setdIsdel(1);
        try {
            departmentService.insertSelective(department2);
            responsibilityService.AddDepResp(new ResponsibilityDepVo(department2.getdId(), resp));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Message.success("部门添加成功");
    }


    /**
     * 查找一个部门
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/findByPrimaryKey.do")
    @ResponseBody
    public Department findByPrimaryKey(int id) throws Exception {
        Department department = departmentService.selectByPrimaryKey(id);
        return department;
    }

    /**
     * 更新部门
     *
     * @param id
     * @param name
     * @param remark
     * @throws Exception
     */
    @RequestMapping("/updateByPrimaryKey.do")
    @ResponseBody
        public Message updateByPrimaryKey(int id, String name, String remark, String level, String gsid, Integer respId, String respContent) {
        if (StringUtils.isEmpty(name)) {
            return Message.error("请填写部门名称");
        }
        Department department = departmentService.findByDname(name);
        if (department != null) {
            if (department.getdId() != id) {
                return Message.error("部门名称不存在");
            }
        }
        Department department2 = new Department();
        department2.setdId(id);
        department2.setdName(name);
        department2.setdLevel(Integer.valueOf(level));
        department2.setdGsid(Integer.valueOf(gsid));
        department2.setdRemark(remark);
        department2.setdIsdel(null);
        departmentService.updateByPrimaryKeySelective(department2);
        if (responsibilityService.checkIfExistExcluedId(id, 0, respId)) {
            return Message.error("该责任已经存在");
        } else {
            if (respId != null){
                responsibilityService.updateResp(new ResponsibilityChangeVo(respId, respContent));
            }
        }
        return Message.success("部门编辑成功");
    }

    @RequestMapping("/findByDname.do")
    @ResponseBody
    public int findByDname(String d_name) {

        Department department = departmentService.findByDname(d_name);
        if (department != null) {
            return department.getdId();
        } else {
            return 0;
        }
    }

    /**
     * 删除部门
     *
     * @param id
     */
    @RequestMapping("/deleteByPrimaryKey.do")
    @ResponseBody
    public Message deleteByPrimaryKey(int id) throws Exception {
        //删除部门，调用更新操作，将状态改为0
        Department department = new Department();
        department.setdId(id);
        department.setdIsdel(0);
        try {
            departmentService.updateByPrimaryKeySelective(department);
        } catch (Exception e) {
            // TODO: handle exception
            return Message.error("删除失败");
        }
        return Message.success("删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @RequestMapping("/deleteByQuery.do")
    public void deleteByQuery(@RequestParam(value = "arr") int[] ids) {
        //批量删除，实则修改状态为0
        //如果有id才执行
        if (ids.length > 0) {
            departmentService.deleteByQuery(ids);
        }

        //        var arr = [3,4,5];
        //        $.ajax({
        //                type:'post',
        //                url:'department/deleteByQuery.do',
        //                data: {"arr":arr},
        //                traditional: true,
        //                dataType:'json',
        //        });

    }

}
